﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class NewClient : BasePage
{
    #region METHODS

    private void ShowError(string errorMessage)
    {
        lblError.Text = errorMessage;
        lblError.Visible = true;
        lblError2.Text = errorMessage + "<br />";
        lblError2.Visible = true;
    }

    #endregion METHODS

    #region EVENTS

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        Validate();
        if (IsValid)
        {
            Customer newCustomer = new Customer();
            newCustomer.City = ctrlCustomer.City;
            newCustomer.Email = ctrlCustomer.Email;
            newCustomer.FirstName = ctrlCustomer.Name;
            newCustomer.Surname = ctrlCustomer.Surname;
            newCustomer.ZipCode = ctrlCustomer.PostCode1;// +"-" + ctrlCustomer.PostCode2;
            newCustomer.Phone = ctrlCustomer.Phone;
            newCustomer.Street = ctrlCustomer.Street + " " + ctrlCustomer.Building + "/" + ctrlCustomer.Flat;
            newCustomer.Country = "Polska";
            newCustomer.PreferedLanguageID = ctrlCustomer.LanguageID;
            newCustomer.NewsSubscription = ctrlCustomer.Newsletter;

            if (ctrlCustomer.SendAbroad)
            {
                newCustomer.CountryID = ctrlCustomer.CountryID;
            }


            if (ctrlCustomer.IsInvoice)
            {
                newCustomer.CompanyName = ctrlCustomer.CompanyName;
                newCustomer.CompanyNIP = ctrlCustomer.NIP;
                if (!ctrlCustomer.SameAddress)
                {
                    newCustomer.CompanyCity = ctrlCustomer.CompanyCity;
                    newCustomer.CompanyStreet = ctrlCustomer.CompanyStreet + " " + ctrlCustomer.CompanyBuilding + " /" + ctrlCustomer.CompanyFlat;
                    newCustomer.CompanyZipCode = ctrlCustomer.CompanyPostCode1;// +"-" + ctrlCustomer.CompanyPostCode2;
                }
                else
                {
                    newCustomer.CompanyCity = newCustomer.City;
                    newCustomer.CompanyStreet = newCustomer.Street;
                    newCustomer.CompanyZipCode = newCustomer.ZipCode;
                }
            }
            else
            {
                newCustomer.CompanyCity = string.Empty;
                newCustomer.CompanyName = string.Empty;
                newCustomer.CompanyNIP = string.Empty;
                newCustomer.CompanyStreet = string.Empty;
                newCustomer.CompanyZipCode = string.Empty;
            }

            MembershipCreateStatus status;
            //bool emailConfirmation = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["NewClientEmailConfirmation"]);
            bool emailConfirmation = ShopModel.Settings.RegistrationConfirmationMail;
            MembershipUser newUser = Membership.CreateUser(ctrlCustomer.Email, ctrlCustomer.Password, ctrlCustomer.Email, ctrlCustomer.Question, ctrlCustomer.Answer, !emailConfirmation, out status);
            if (status == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(newUser.UserName, "customer");

                if (OrderManager.Instance.AddCustomerToProfile((Guid)newUser.ProviderUserKey, newCustomer))
                {
                    if (emailConfirmation)
                    {
                        try
                        {
                            ReportsManager.Instance.SendAccountCreatedConfirmationEmail(newUser, Context);
                        }
                        catch
                        {
                            lblInfo.Text = Resources.Language.NewClient_SendMailError + Settings.CompanyEmail;
                        }

                        pnlNewClient.Visible = false;
                        pnlInfo.Visible = true;
                    }
                    else
                    {
                        lblInfo.Text = string.Format(Resources.Language.NewClient_AccountCreatedConfirmation, Resources.Language.UrlLogin.ToLower()+".aspx");
                        pnlNewClient.Visible = false;
                        pnlInfo.Visible = true;
                    }
                }
                else
                {
                    try
                    {
                        Membership.DeleteUser(newUser.UserName, true);
                    }
                    catch { }
                    finally
                    {
                        ShowError(Resources.Language.Error_PleaseTryLater);
                    }
                }
            }
            else
            {
                switch (status)
                {
                    case MembershipCreateStatus.DuplicateEmail:
                    case MembershipCreateStatus.DuplicateUserName:
                        ShowError(Resources.Language.NewClient_EmailDuplicate);
                        break;

                    case MembershipCreateStatus.InvalidUserName:
                    case MembershipCreateStatus.InvalidEmail:
                        ShowError(Resources.Language.NewClient_EmailInvalid);
                        break;

                    case MembershipCreateStatus.InvalidPassword:
                        ShowError(Resources.Language.NewClient_PasswordInvalid);
                        break;

                    case MembershipCreateStatus.InvalidQuestion:
                        ShowError(Resources.Language.NewClient_QuestionInvalid);
                        break;

                    case MembershipCreateStatus.InvalidAnswer:
                        ShowError(Resources.Language.NewClient_AnswerInvalid);
                        break;

                    default:
                        ShowError(Resources.Language.Error_PleaseTryLater);
                        break;

                }
            }
        }
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipUser user;
        if (!IsPostBack && this.User.Identity.IsAuthenticated)
        {
            Response.Redirect(Resources.Language.UrlDefault.ToLower()+".aspx");
        }

        if (Request.QueryString["user"] != null && Request.QueryString["confirm"] != null)
        {
            pnlNewClient.Visible = false;
            pnlInfo.Visible = true;

            string userName = HttpUtility.UrlDecode(Request.QueryString["user"]);
            user = Membership.GetUser(userName);
            //if (usersCollection != null && usersCollection.Count > 0)
            if (user != null)
            {
                //user = usersCollection[userName];

                if ((Guid)user.ProviderUserKey == new Guid(HttpUtility.UrlDecode(Request.QueryString["confirm"])))
                {
                    user.IsApproved = true;
                    Membership.UpdateUser(user);
                    lblInfo.Text = string.Format(Resources.Language.NewClient_AccountCreatedConfirmation, Resources.Language.UrlLogin.ToLower()+".aspx");
                    bool emailConfirmation = ShopModel.Settings.RegistrationConfirmationMail;
                    if (emailConfirmation)
                    {
                        try
                        {
                            ReportsManager.Instance.SendAccountActivatedConfirmationEmail(user, Context);
                        }
                        catch { }
                    }
                }
                else
                {
                    lblInfo.Text = Resources.Language.NewClient_AccountCreatedConfirmationFault;
                }
            }
            else
            {
                lblInfo.Text = Resources.Language.NewClient_AccountCreatedConfirmationFault;
            }
        }
    }

    #endregion PAGE EVENTS
}
